require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#searchPage",
    data:{
        type:0,
        indexs:1,
        name:"",
        loginUid:userInfo.id,
        searchList:{},
        chcircleList:{},
        friendList:{},
        clenotinList:{}
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.num=this.searchObject&&this.searchObject.indexs;
    },
    methods:{
        //后退
        getBack: function () {
            if(this.num){
                location.href="/friend?indexs="+this.num;
                // history.go(-1);
            }else{
                this.name="";
                this.type=0;
                this.indexs=1;
            }

        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //搜索结果
        search:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                // loginUid:1,
                name:that.name
            };
            this.ajax({url:"/friend/search",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.searchList=b.json.data;
                    //vm.getList(1);
                    //console.log(that.friendList)
                }else{
                    vm.$refs.prompt.toast("搜索失败！")
                }
            });
        },
        //搜索圈子
        searchcircleList:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                // loginUid:1,
                name:that.name
            };
            this.ajax({url:"/friend/searchcircleList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.chcircleList=b.json.data;
                    //vm.getList(1);
                    //console.log(that.friendList)
                }else{
                    vm.$refs.prompt.toast("搜索圈子失败！")
                }
            });
        },
        //搜索好友
        searchfriendList:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                // loginUid:1,
                name:that.name
            };
            this.ajax({url:"/friend/searchfriendList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.friendList=b.json.data;
                    //vm.getList(1);
                    //console.log(that.friendList)
                }else{
                    vm.$refs.prompt.toast("搜索好友失败！")
                }
            });
        },
        //搜索未加入圈子
        searchcirclenotinList:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                // loginUid:1,
                name:that.name
            };
            console.log(data)
            this.ajax({url:"/friend/searchcirclenotinList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.clenotinList=b.json.data;
                    //vm.getList(1);
                    //console.log(that.friendList)
                }else{
                    vm.$refs.prompt.toast("搜索圈子列表失败！")
                }
            });
        },
        getReplace:function(name){
           return name.split(this.name).join("<span>"+this.name+"</span>")
        },
        getTab:function(num){
            this.indexs="";
            this.type=num;
        },
        getMore:function(num){
            if(num==1){
                this.type=1;
                this.indexs=3;
                this.searchfriendList();
            }else if(num==2){
                this.type=2;
                this.indexs=5;
                this.searchcirclenotinList();
            }
        },
        //加入圈子
        joinGroup:function(circleId){
            var that=this;
            var data={
                loginUid:that.loginUid,
                // loginUid:1,
                circleId:circleId
            };
            console.log(data)
            this.ajax({url:"/friend/joinGroup",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.searchcirclenotinList();
                    //vm.getList(1);
                    //console.log(that.friendList)
                }else{
                    vm.$refs.prompt.toast("加入圈子失败！")
                }
            });
        },
        //跳转页面
        getNext:function(list,type){
            var path;
            if(type==1){
                path="/userHomepage?id="+list.uid;
            }else if(type==2){
                path="/friend/circleBar?type="+type+"&circleId="+list.circleId+"&uid="+userInfo.id;
            }
            location.href=path;
        }
    },
    watch:{
        name:function(){
            if(this.name){
                if(this.type==0){
                    this.indexs=2;
                    this.search();
                }else if(this.type==1){
                    this.indexs=3;
                    this.searchfriendList();
                }else if(this.type==2){
                    this.indexs=4;
                    this.searchcircleList();
                }
            }else{
                this.indexs=1;
            }
        }
    }
});